Conditions | 17 |
Paths | 55 |
Total Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like formManager.getFormFields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var formManager = function(){ |
||
80 | getFormFields: function (form, isSubmission) { |
||
81 | var usernameField = null; |
||
82 | |||
83 | // Locate the password field(s) in the form. Up to 3 supported. |
||
84 | // If there's no password field, there's nothing for us to do. |
||
85 | var pwFields = this._getPasswordFields(form, isSubmission); |
||
86 | if (!pwFields){ |
||
87 | return [null, null, null]; |
||
88 | } |
||
89 | |||
90 | |||
91 | // Locate the username field in the form by searching backwards |
||
92 | // from the first passwordfield, assume the first text field is the |
||
93 | // username. We might not find a username field if the user is |
||
94 | // already logged in to the site. |
||
95 | for (var i = pwFields[0].index - 1; i >= 0; i--) { |
||
96 | if (form.elements[i].type.toLowerCase() === "text" || form.elements[i].type.toLowerCase() === "email") { |
||
97 | usernameField = form.elements[i]; |
||
98 | break; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | if (!usernameField){ |
||
103 | this.log('(form ('+ form.action +') ignored -- no username field found)'); |
||
104 | } |
||
105 | |||
106 | |||
107 | // If we're not submitting a form (it's a page load), there are no |
||
108 | // password field values for us to use for identifying fields. So, |
||
109 | // just assume the first password field is the one to be filled in. |
||
110 | if (!isSubmission || pwFields.length === 1){ |
||
111 | var res = [usernameField, pwFields[0].element]; |
||
112 | if(pwFields[1]){ |
||
113 | res.push(pwFields[1].element); |
||
114 | } else { |
||
115 | res.push(null); |
||
116 | } |
||
117 | return res; |
||
118 | } |
||
119 | |||
120 | |||
121 | |||
122 | // Try to figure out WTF is in the form based on the password values. |
||
123 | var oldPasswordField, newPasswordField; |
||
124 | var pw1 = pwFields[0].element.value; |
||
125 | var pw2 = pwFields[1].element.value; |
||
126 | var pw3 = (pwFields[2] ? pwFields[2].element.value : null); |
||
127 | |||
128 | if (pwFields.length === 3) { |
||
129 | // Look for two identical passwords, that's the new password |
||
130 | |||
131 | if (pw1 === pw2 && pw2 === pw3) { |
||
132 | // All 3 passwords the same? Weird! Treat as if 1 pw field. |
||
133 | newPasswordField = pwFields[0].element; |
||
134 | oldPasswordField = null; |
||
135 | } else if (pw1 === pw2) { |
||
136 | newPasswordField = pwFields[0].element; |
||
137 | oldPasswordField = pwFields[2].element; |
||
138 | } else if (pw2 === pw3) { |
||
139 | oldPasswordField = pwFields[0].element; |
||
140 | newPasswordField = pwFields[2].element; |
||
141 | } else if (pw1 === pw3) { |
||
142 | // A bit odd, but could make sense with the right page layout. |
||
143 | newPasswordField = pwFields[0].element; |
||
144 | oldPasswordField = pwFields[1].element; |
||
145 | } else { |
||
146 | // We can't tell which of the 3 passwords should be saved. |
||
147 | this.log("(form ignored -- all 3 pw fields differ)"); |
||
148 | return [null, null, null]; |
||
149 | } |
||
150 | } else { // pwFields.length == 2 |
||
151 | if (pw1 === pw2) { |
||
152 | // Treat as if 1 pw field |
||
153 | newPasswordField = pwFields[0].element; |
||
154 | oldPasswordField = null; |
||
155 | } else { |
||
156 | // Just assume that the 2nd password is the new password |
||
157 | oldPasswordField = pwFields[0].element; |
||
158 | newPasswordField = pwFields[1].element; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return [usernameField, newPasswordField, oldPasswordField]; |
||
163 | }, |
||
164 | log: function (str) { |
||
239 | formManager._init_(); |